package com.xbjtech.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.xbjtech.model.Resource;
import com.xbjtech.model.beans.Result;
import com.xbjtech.service.ResourceService;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

@Controller
@RequestMapping("/resource")
@Slf4j
public class ResourceController {
	
	@Autowired
	ResourceService resourceService;
	
	@GetMapping("/list")
	public String list() {
		return "resource/list";
	}
	
	@GetMapping(value = "/data", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> data() {
//		Example ex = new Example(Resource.class);
//		Criteria criteria = ex.createCriteria();
//		if (null != name && !"".equals(name)) {
//			criteria.andLike("name", "%" + name + "%");
//		}
//		if (null != code && !"".equals(code)) {
//			criteria.andEqualTo("code", code);
//		}
		List<Resource> res = resourceService.selectByExample(null);
		Map<String, Object> map = new HashMap<>();
		map.put("total", res.size());
		map.put("rows", res);
		return map;
	}
	
	@RequestMapping("/selectdata")
	@ResponseBody
	public String selectdata() {
		// 会有性能问题
		// 查询所有的根节点数据
		Example ex = new Example(Resource.class);
		ex.createCriteria().andIsNull("parId");
		List<Resource> res = resourceService.selectByExample(ex);
		for (Resource r : res) {
			// 遍历方式查出所有的二级节点数据
			Example e = new Example(Resource.class);
			e.createCriteria().andEqualTo("parId", r.getId());
			List<Resource> children = resourceService.selectByExample(e);
			r.setChildren(children);
		}
		return JSON.toJSONString(res);
	}
	
	@RequestMapping("/selectthreeleveldata")
	@ResponseBody
	public String selectThreeLevelData() {
		// 会有性能问题
		// 查询所有的根节点数据
		Example ex = new Example(Resource.class);
		ex.createCriteria().andIsNull("parId");
		List<Resource> res = resourceService.selectByExample(ex);
		for (Resource r : res) {
			// 遍历方式查出所有的二级节点数据
			Example e = new Example(Resource.class);
			e.createCriteria().andEqualTo("parId", r.getId());
			List<Resource> children = resourceService.selectByExample(e);
			for (Resource temp : children) {
				// 遍历方式查出所有的三级级节点数据
				Example example = new Example(Resource.class);
				example.createCriteria().andEqualTo("parId", temp.getId());
				List<Resource> c = resourceService.selectByExample(example);
				temp.setChildren(c);
			}
			r.setChildren(children);
		}
		return JSON.toJSONString(res);
	}
	
	@PostMapping("/save")
	@ResponseBody
	public Result save(@Validated Resource resource, Errors errors, Model model) {
		// 服务器端的校验没有做【我建议是要做服务器端的校验】
		// 
		List<FieldError> filedErros = errors.getFieldErrors();
		for (FieldError error : filedErros) {
			model.addAttribute(error.getField() + "ERROR", error.getDefaultMessage());
		}
		try {
			if (null == resource.getId()) {
				resourceService.insertWithoutPrimaryKey(resource);
			} else {
				resourceService.updateByPrimaryKeySelective(resource);
			}
//			if (true)
//			throw new RuntimeException();
			// 新增成功 修改成功
			return Result.success();
		} catch (RuntimeException e) {
			// 新增失败 修改失败
			log.error("ResourceController.save发生了异常=>{}", e.getMessage());
		}
		return Result.fail();
	}
	
	@GetMapping("/del")
	@ResponseBody
	public Result del(
			@RequestParam(required = true)
			Integer id) {
		try {
			resourceService.deleteByPrimaryKey(id);
			return Result.success();
		} catch (RuntimeException e) {
			log.error("ResourceController.del() throws exception:{}", e.getMessage());
		}
		return Result.fail();
	}

}
